<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新比赛</h2>

    <el-steps :active="1" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写比赛基本信息"/>
      <el-step title="选择比赛开设项目"/>
      <el-step title="最终发布"/>
    </el-steps>

          <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px"
        label-position="left">
        <el-col :span="13">
          <el-form-item label="比赛名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入比赛名" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="比赛地点" prop="location">
            <el-input v-model="formData.location" placeholder="请输入比赛地点" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="比赛时间" prop="time">
            <el-date-picker v-model="formData.time" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
              :style="{width: '100%'}" placeholder="请选择比赛时间" clearable></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="最大人数" prop="maxNum">
            <el-input v-model="formData.maxNum" placeholder="请输入最大人数" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="赛制表" prop="schedule" required>
            <el-upload ref="schedule"
              :action="BASE_API + '/oss/upload'"
              :before-upload="scheduleBeforeUpload"
              :limit="1"
              :on-success="successUpload"
              >
              <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <img class="schedule" :src="imgUrl">
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">下一步</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
  </div>
</template>

<script>
import comp from '@/api/comp/comp'
export default {
  data() {
    return {
      formData: {
        name: undefined,
        location: undefined,
        time: null,
        maxNum: undefined,
        schedule: '',
      },
      rules: {
        name: [{
          required: true,
          message: '请输入比赛名',
          trigger: 'blur'
        }],
        location: [{
          required: true,
          message: '请输入比赛地点',
          trigger: 'blur'
        }],
        time: [{
          required: true,
          message: '请选择比赛时间',
          trigger: 'change'
        }],
        maxNum: [{
          required: true,
          message: '请输入最大人数',
          trigger: 'blur'
        }],
      },
      BASE_API:process.env.BASE_API,
      compId:undefined,
      imgUrl:'',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        // 判断添加还是修改
        if (this.compId) {
          this.updateComp();
        } else {
          this.addComp();
        }
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
      this.imgUrl = ''
    },
    scheduleBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    scheduleAction(file) {
      console.log(111);
    },
    init() {
      console.log("created");
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.getInfo(id)
        this.compId = id
      } else {
        this.formData = {};
      }
    },
    addComp() {
      // TODO 提交表单
      comp.addComp(this.formData).
      then((response) => {
        console.log(response);
      //提示添加成功
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到列表界面 路由跳转 => 重定向
        this.$router.push({ path: "/comp/add2/" + response.data.compId });
      })
      .catch((error) => {
        console.log(error)
      });
    },
    successUpload(response) {
      console.log("uploadsuccess");
      this.formData.schedule = response.data.url
      this.imgUrl = response.data.url
    },
    getInfo(id) {
      comp.getCompById(id).
      then((response) => {
        this.formData = response.data.comp
        this.imgUrl = response.data.comp.schedule
      })
      .catch((error) => {
        console.log(error)
      });
    },
    updateComp(){
      comp.updateComp(this.formData).
      then((response) => {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到列表界面 路由跳转 => 重定向
        this.$router.push({ path: "/comp/add2/" + this.compId });
      })
      .catch((error) => {
        console.log(error)
      });
    }
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
.schedule {
  width: 1050px;
  height: 600px;
}

</style>
